﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Data.Interfaces;
using Data.Interfaces.Callback;
using Data;
using System.ServiceModel;

namespace Server.Services
{
    class PlayerService:IPlayerService
    {
        public void UpdatePlayer(string pseudo, string classePerso, int posX, int posY, int orientation, int step, int life, int mana)
        {
            ICPlayerService currentCallback = OperationContext.Current.GetCallbackChannel<ICPlayerService>();
            if (!MainWindow.listPlayerCallBacks.Contains(currentCallback))
                MainWindow.listPlayerCallBacks.Add(currentCallback);

            foreach (Player playerLoc in MainWindow.ListPlayers)
            {
                if (playerLoc.pseudo == pseudo)
                {
                    playerLoc.character.name = classePerso;
                    playerLoc.posX = posX;
                    playerLoc.posY = posY;
                    playerLoc.orientation = orientation;
                    playerLoc.step = step;
                    playerLoc.character.life = life;
                    playerLoc.character.mana = mana;
                    
                }
            }
        }

        public void UpdateAnimation(string pseudo, int posX, int posY,int width, int height, string attackType, int orientation)
        {
            Player attaquant = null;
            int marginXleft = 0;
            int marginXright = 0;
            int marginYup = 0;
            int marginYdown = 0;
            int damage = 0;
            ICPlayerService currentCallback = OperationContext.Current.GetCallbackChannel<ICPlayerService>();
            if (!MainWindow.listPlayerCallBacks.Contains(currentCallback))
                MainWindow.listPlayerCallBacks.Add(currentCallback);

            foreach (ICPlayerService icp in MainWindow.listPlayerCallBacks)
            {
                icp.updateOtherAnimation(pseudo, posX, posY, attackType, orientation);
            }

            //cékikitape
            foreach (Player player in MainWindow.ListPlayers)
            {
                if (pseudo == player.pseudo)
                {
                    attaquant = player;
                    switch(orientation)
                    {
                        case 0:
                            marginYdown = 24;
                            break;

                        case 1:
                            marginXleft = 24;
                            break;

                        case 2:
                            marginXright = 24; 
                            break;

                        case 3:
                            marginYup = 24;
                            break;
                    }
                                                                 
                }
            }

            //calcul hit box
            foreach(Player player in MainWindow.ListPlayers)
            {
                bool touched = false;
                if (pseudo != player.pseudo && player.character.life > 0)
                {

                    if (attackType == "sword" || attackType == "circlefire" || attackType == "execution")
                    {
                        #region hitbox swordAndcirclefire

                        // calcul horizontal
                        if ((attaquant.posX  > player.posX
                            && attaquant.posX < player.posX + player.character.spriteX)
                            || ((attaquant.posX+attaquant.character.spriteX)/2 > player.posX
                            && (attaquant.posX + attaquant.character.spriteX) / 2 < player.posX + player.character.spriteX)
                            || ((attaquant.posX+attaquant.character.spriteX) > player.posX
                            && attaquant.posX + attaquant.character.spriteX < player.posX + player.character.spriteX))
                        {
                            //calcul vertical
                            if ((attaquant.posY  > player.posY
                            && attaquant.posY < player.posY + player.character.spriteY)
                            || ((attaquant.posY + attaquant.character.spriteY)/2 > player.posY
                            && (attaquant.posY + attaquant.character.spriteY) / 2 < player.posY + player.character.spriteY)
                            || ((attaquant.posY + attaquant.character.spriteY) > player.posY
                            && attaquant.posY + attaquant.character.spriteY < player.posY + player.character.spriteY))
                    
                            {
                                touched = true;
                            }
                        }
                        #endregion
                    }
                    else if (attackType == "boltfire")
                    {
                        #region hitbox boltfire

                        int playPosx = 0;
                        int playPosy = 0;
                        int spriteX = 0;
                        int spriteY = 0;

                        switch (orientation)
                        {
                            case 0:
                                spriteY = 250;
                                break;
                            case 1:
                                playPosx = -250;
                                spriteX = 250;
                                break;
                            case 2:
                                spriteX = 250;
                                break;
                            case 3:
                                playPosy = -250;
                                spriteY = 250;
                                break;
                        }


                        //Calcul horizontal
                        if (
                            (player.posX > attaquant.posX + playPosx 
                            && player.posX < attaquant.posX + playPosx + attaquant.character.spriteX + spriteX)
                            || (player.posX + player.character.spriteX > attaquant.posX + playPosx + spriteX 
                            && player.posX + player.character.spriteX < attaquant.posX + playPosx + attaquant.character.spriteX + spriteX)
                            )
                        {
                            if (
                            (player.posY > attaquant.posY + playPosy
                            && player.posY < attaquant.posY + playPosy + attaquant.character.spriteY + spriteY)
                            || (player.posY + player.character.spriteY > attaquant.posY + playPosy + spriteY
                            && player.posY + player.character.spriteY < attaquant.posY + playPosy + attaquant.character.spriteY + spriteY)
                            )
                                touched = true;
                        }

                        #endregion
                    }
                    else if (attackType == "confession")
                    {
                        
                        if (attaquant.pseudo == pseudo)
                        {
                            attaquant.character.life = attaquant.character.life + 200;
                            if (attaquant.character.life > attaquant.character.maxLife)
                                attaquant.character.life = attaquant.character.maxLife;
                        }
                        touched = false;

                        foreach (ICPlayerService icp in MainWindow.listPlayerCallBacks)
                        {
                            icp.updateCurrentPlayerLife(attaquant.pseudo, attaquant.character.life, 0);
                        }
                    }
                    //Si on touche l'adversaire
                    if (touched)
                    {
                        if (attackType == "execution")
                        {
                            if (player.character.life > 0)
                            {
                                damage = player.character.life / 2;
                                player.character.life = player.character.life / 2;
                            }
                        }
                        else
                            damage = player.character.LooseLife(attaquant.character.attack);

                        //scores
                        if (player.character.life <= 0)
                        {
                            player.death++;
                            attaquant.score++;

                        }

                        // UpdatePlayer(player.pseudo, player.character.name, player.posX, player.posY, player.orientation, player.step, player.character.life, player.character.mana);
                        foreach (ICPlayerService icp in MainWindow.listPlayerCallBacks)
                        {
                            icp.updateCurrentPlayerLife(player.pseudo, player.character.life, damage);
                            icp.updatePlayerScore(player.pseudo, player.death, player.score);
                            icp.updatePlayerScore(attaquant.pseudo, attaquant.death, attaquant.score);
                        }

                    }
                }
            }
            

        }


    }
}
